//
//  KKSupplyOrderItemList.m
//  FreightAssistant
//
//  Created by Mac mini on 15/12/16.
//  Copyright © 2015年 leo. All rights reserved.
//

#import "KKSupplyOrderItemList.h"

@implementation KKSupplyOrderItemList

- (void)appendItemsWithArray:(NSArray *)itemArray {
    
    [super appendItemsWithArray: itemArray withItemClass: [KKSupplyOrderItem class]];
}

@end

#pragma mark -
#pragma mark - KKSupplyOrderItem
@interface KKSupplyOrderItem() {
    
    NSString *_orderDescription;
}

@end

@implementation KKSupplyOrderItem

#pragma mark - Setter methods

- (void)setKk_customerItem:(NSDictionary *)kk_customerItem {
    
    _kk_customerItem = [KKCustomerItem new];
    [_kk_customerItem kk_parserJsonDictionary: kk_customerItem];
}

- (void)setKk_logisticsItem:(NSDictionary *)kk_logisticsItem {
    
    _kk_logisticsItem = [KKLogisticsItem new];
    [_kk_logisticsItem kk_parserJsonDictionary: kk_logisticsItem];
    
    self.kk_logisticsAmount = kk_logisticsItem[@"amount"];
    self.kk_logisticsNumber = kk_logisticsItem[@"number"];    
}

- (void)setKk_suppplyCommodityItems:(NSArray *)kk_suppplyCommodityItems {
    
    NSMutableArray *items = [NSMutableArray array];
    
    for (NSDictionary *itemDictionary in kk_suppplyCommodityItems) {
        
        KKSupplyCommodityItem *commodityItem = [KKSupplyCommodityItem new];
        [commodityItem kk_parserJsonDictionary: itemDictionary];
        [items addObject: commodityItem];
    }
    _kk_suppplyCommodityItems = [NSArray arrayWithArray: items];
}

#pragma mark -

- (double)orderAggregateAmount {
    
    return [self.kk_logisticsAmount doubleValue] + [[self kk_amount] doubleValue];
}

- (NSString *)orderDescription {
    
    if (_orderDescription == nil) {
        
        NSInteger commodityTotalCount = 0;
        
        for (KKSupplyCommodityItem *item in _kk_suppplyCommodityItems) {
            commodityTotalCount += [[item kk_number] integerValue];
        }
        
        _orderDescription = [NSString stringWithFormat: @"共%@种%@件商品",@(_kk_suppplyCommodityItems.count),@(commodityTotalCount)];
    }
    
    return _orderDescription;
}

@end